<template>
  <div class='upload'>
    <div class="title">标题:</div>
    <div class="file">
       <el-upload
  class="upload-demo"
  action="#"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :before-remove="beforeRemove"
  multiple
  :limit="3"
  :on-exceed="handleExceed"
  :file-list="fileList"
  :http-request="httpRequest">
  
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">支持扩展名：xls、xlsx，文件不得大于1M</div>
</el-upload>
    </div>
  </div>
</template>

<script>
import {importstata} from '@/api/commodity'
import * as XLSX from 'xlsx'
export default {
  name: 'UploadExcel',

   data() {
      return {
        fileList: []
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
       async httpRequest({file}){
         console.log(file,'111');
         // 转为二进制
      const formData = new FormData()
      formData.append('fileName',file)
      console.log(formData,'formData');
      const res = await importstata(formData)
      console.log(res,'11111111111');
    }
    }
};
</script>

<style lang="scss" scoped>
.upload{
display: flex;
.title{
  margin-left: 80px;
  font-size: 20px;
}
.file{
  margin-left: 13px;
  ::v-deep .el-button {
    width: 200px;
    height: 30px;
  }
}
}
</style>